package com.ayoka.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.ayoka.R;
import com.jcp.baselibrary.utils.UIUtils;
import com.jincp.image.load.ImageLoaderOptions;
import com.jincp.image.load.ImageLoaderProxy;

import java.util.ArrayList;
import java.util.List;

/**
 * =========================================================
 * 作者： 金春平
 * 类名： ActionAdapter
 * 类描述：
 * 创建日期 16/9/28
 * =========================================================
 */
public abstract class GuidePagerAdapter extends PagerAdapter {

    private Context context;
    private List<View> viewContainer = new ArrayList<>();
    private List<Integer> data;

    public GuidePagerAdapter(Context context, List<Integer> data) {
        this.context = context;
        this.data = data;
        initGuide();
    }

    private void initGuide() {
        for (int i = 0; i < data.size(); i++) {
            View view = View.inflate(context, R.layout.viewpager_item_guide, null);
            viewContainer.add(view);
        }
    }

    @Override
    public int getCount() {
        return viewContainer.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(viewContainer.get(position));
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = viewContainer.get(position);
        ImageView imageView = (ImageView) view.findViewById(R.id.iv_picture);
        ImageLoaderProxy.getInstance().displayImage(data.get(position), imageView, new ImageLoaderOptions.Builder().setDefaultDrawable(data.get(position)).build());
        TextView tvTitle = (TextView) view.findViewById(R.id.tv_title);
        TextView tvVerticalTitle = (TextView) view.findViewById(R.id.tv_vertical_title);
        tvVerticalTitle.setText(getVerticalTitle(position));
        Button btnLogin = (Button) view.findViewById(R.id.btn_wx_login);
        btnLogin.setOnClickListener(getListener());
        if (position + 1 == data.size()){
            btnLogin.setVisibility(View.VISIBLE);
        } else {
            btnLogin.setVisibility(View.GONE);
        }
        container.addView(view, 0);
        return viewContainer.get(position);
    }

    private SpannableStringBuilder getVerticalTitle(int position) {
        SpannableStringBuilder builder = null;
        switch (position) {
            case 0:
                builder = new SpannableStringBuilder("焦虑的父母\n 失控的孩子");
                builder.setSpan(new ForegroundColorSpan(UIUtils.parseColor("#E6001C")), 7, 9, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                break;
            case 1:
                builder = new SpannableStringBuilder("焦虑的父母\n 抑郁的孩子");
                builder.setSpan(new ForegroundColorSpan(UIUtils.parseColor("#177FFF")), 7, 9, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                break;
            case 2:
                builder = new SpannableStringBuilder("平和的父母\n 友善的孩子");
                builder.setSpan(new ForegroundColorSpan(UIUtils.parseColor("#20FFFD")), 7, 9, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                break;
            case 3:
                builder = new SpannableStringBuilder("平和的父母\n 快乐的孩子");
                builder.setSpan(new ForegroundColorSpan(UIUtils.parseColor("#FDA114")), 7, 9, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                break;
            default:
                break;
        }
        return builder;
    }

    protected abstract View.OnClickListener getListener();
}
